


import {baseURL} from '@/util/util';
import upload from '@/components/upload'
import imgPreviewfrom from '@/components/imagePreview'
 export default {
    data() {
        return {
          isOpenImg:false,
          previewUrl:''
        }
    },
		computed: {
      /* @param {Array | String} data 数据
      * @return {Array} 返回数据
       */
      imageUploadList() {
        return (data) =>
         typeof data === 'string' && data.length ? JSON.parse(data) : data
      },
      imageUploadlength() {
        return (data, num) => {
          if (!data) return true
          const list =
            typeof data === 'string' && data.length ? JSON.parse(data) : data
          return list.length < num
        }
    },
		},
    methods: {
      /* 预览
      * @param {String} url url字段
      * @param {Boolean} preview字段
       */
      openImg(url, preview,identifier) {
        this.$refs[`imagePeeview${identifier}`].clickOpen(url, preview)
      },
      /* 删除单一上传的图片
      * @param {Object} data 数据
      * @param {String} url url字段
      * @param {String} name name字段
       */
      imageUploadDeleteItem(data,url,name){
        if(url) data[url] = ""
        if(name) data[url] = ""
      },

        	
      /* 上传一个图片
      * @param {Object} data 数据
      * @param {String} url url字段
      * @param {String} name name字段
       */
      imageUploadChangeItem(file,data,url,name,identifier){
          data[url] = file.url
          data[name] = file.title
          if(this[`uploadSuccess${identifier}`]){
						this[`uploadSuccess${identifier}`]();
					}
      },

      /* 上传前判断是否是图片
       * @param {object} file 文件
       * @param {number} maxSize 最大文件
       */
      imageUploadBefore(file,maxSize){
        const isJpgOrPng = ["image/jpeg","image/png","image/webp","image/bmp","image/gif","image/svg"].includes(file.type)
    
        if (!isJpgOrPng) {
          this.$message.error("只能上传图片格式!");
          return false;
        }
        const isLtMaxSize = file.size / 1024 / 1024 < maxSize
        if (!isLtMaxSize) {
          this.$message.error(`图片大小不能超过${maxSize}MB!`);
        }
        return isJpgOrPng && isLtMaxSize;
      },

      /* 删除某一项图片
       * @param {object} imageItem 一项图片
       * @param {object} dataSource 数据
       * @param {String} fieldId 字段
       */
      imageUploadDeleteList(imageItem,dataSource,fieldId){
        const imageList = this.imageUploadList(dataSource[fieldId])
        const index = imageList?.findIndex(item => item.url === imageItem.url)
        if(index >= 0){
          imageList.splice(index,1)
          dataSource[fieldId] = JSON.stringify(imageList)
        }
      },

      /* @param {object} info 文件
       * @param {object} dataSource 数据
       * @param {String} fieldId 字段
       */
      imageUploadChange(info,dataSource,fieldId,identifier){
          const imageList = this.imageUploadList(dataSource[fieldId])
          imageList.push(info)
          dataSource[fieldId] = JSON.stringify(imageList)
        if(this[`uploadSuccess${identifier}`]){
            this[`uploadSuccess${identifier}`]();
        }
      },

      imageUploadError(){
				this.$message.error('上传失败')
      },

    },
    components:{
			upload,
      imgPreviewfrom
		}
 
    }
